package controller;

import com.root.health.bean.*;
import com.root.health.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.*;

@Controller
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    CorrelationService correlationService;
    @Autowired
    MedicationService medicationService;
    @Autowired
    HealthInfoService healthInfoService;
    @Autowired
    CorrelationController correlationController;
    @Autowired
    HealthInfoController healthInfoController;
    @Autowired
    ExamService examService;
    @Autowired
    MedicationController medicationController;


    @GetMapping(value = {"/","login"})
    public String loginPage(){

        return "login";
    }
    @PostMapping("login")
    public String loginPage(UserInfo userInfo, HttpSession session, Model model){
        //测试用System.out.println(userInfo.getUserName()+userInfo.getPassword());
        UserInfo checkuser = userService.getUserbyName(userInfo.getUserName());//从数据库查询对应用户名的用户信息
        if(checkuser == null){
            //如果查询到的用户信息为空，传递message用户名错误
            model.addAttribute("msg","用户名错误");
            //回到登录页面
            return "login";
        }
        if(checkuser.getPassword().equals(userInfo.getPassword())){
            //如果查询到的用户信息中密码与表单传递来的密码相同，则登录成功

            String gender = checkuser.getGender();
            String role = checkuser.getRole();
            String headimg_src;
            if(gender.equals("male")){
                if(role.equals("children")){
                    headimg_src = "images/headimage/young_man.png";
                }else{
                    headimg_src = "images/headimage/elder_man.png";
                }
            }else{
                if(role.equals("children")){
                    headimg_src = "images/headimage/young_woman.png";
                }else{
                    headimg_src = "images/headimage/elder_woman.png";
                }
            }//设置用户头像
            if(checkuser.getRole().equals("children")){
                checkuser.setRole("子女");
            }else if(checkuser.getRole().equals("parents")){
                checkuser.setRole("父母");
            }else{
                model.addAttribute("errormsg","登录用户角色错误");
                return "error";
            }
            if(checkuser.getGender().equals("male")){
                checkuser.setGender("男");
            }else if(checkuser.getGender().equals("female")){
                checkuser.setGender("女");
            }else{
                model.addAttribute("errormsg","登录用户性别错误");
                return "error";
            }
            session.setAttribute("loginUser",checkuser);
            session.setAttribute("loginUser_headimg_src",headimg_src);
            //把登录成功的用户保存
            return "login_success";
        } else{
            //否则登录失败，传递message密码错误
            model.addAttribute("msg","密码错误");
            //回到登录页面
            return "login";
        }
    }
    @GetMapping("main")
    public String mainPage(HttpSession session,Model model){
        UserInfo checkuser = (UserInfo) session.getAttribute("loginUser");
        if(checkuser == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }
        //把登录成功的用户保存
        Date dateNow = new Date(System.currentTimeMillis());//获取系统时间
        List<Map<String, Object>> userMapList;
        userMapList = correlationController.ChildrenCorrUser(checkuser);
        List<Map<String, Object>> examMsgMapList = new LinkedList<>();
        List<Map<String, Object>> medMsgMapList = new LinkedList<>();
        List<Map<String,Object>> healthWarningMapList = new ArrayList<>();
        //消息提示功能
        Map<String, Object> noticeMsgMap;
        if(checkuser.getRole().equals("子女")) {
            for (int i = 0; i < userMapList.toArray().length; i++) {
                List<ExamInfo> examList = examService.examByid((Long) userMapList.get(i).get("userid"));//获取体检信息
                String realname = (String) userMapList.get(i).get("realname");
                ExamInfo examInfo;
                for (int j = 0; j <examList.toArray().length; j++) {
                    examInfo = examList.get(j);
                    java.util.Date examDate = examInfo.getExamDate();
                    if (examInfo.getExamState()==0) {
                        if(examDate.getTime()-dateNow.getTime()<=1800000){
                            if(examDate.getTime()-dateNow.getTime()<0){
                                String examMsg = realname+"该去"+examInfo.getExamSite()+"做"+examInfo.getMain()+"了";
                                //System.out.println(examMsg);
                                String examImg = "images/msg/exam1.png";
                                String examClass = " alert alert-block alert-danger fade in";
                                String examLabelClass = "label label-danger";
                                String examIClass = "fa fa-bolt";
                                ExamInfo finalExamInfo = examInfo;
                                noticeMsgMap = new HashMap<>(){{
                                    put("examId", finalExamInfo.getExamId());
                                    put("examNotice",examMsg);
                                    put("examImg",examImg);
                                    put("examClass",examClass);
                                    put("examLabelClass",examLabelClass);
                                    put("examIClass",examIClass);
                                }};
                                examMsgMapList.add(noticeMsgMap);
                            }
                            else {
                                long time = (examDate.getTime()-dateNow.getTime())/60000;
                                String examMsg = "还有"+time+"分钟"+realname+"就该去"+examInfo.getExamSite()+"做"+examInfo.getMain()+"了";
                                //System.out.println(examMsg);
                                String examImg = "images/msg/exam1.png";
                                String examClass = "alert alert-warning fade in";
                                String examLabelClass = "label label-warning";
                                String examIClass = "fa fa-clock-o";
                                ExamInfo finalExamInfo1 = examInfo;
                                noticeMsgMap = new HashMap<>(){{
                                    put("examId", finalExamInfo1.getExamId());
                                    put("examNotice",examMsg);
                                    put("examImg",examImg);
                                    put("examClass",examClass);
                                    put("examLabelClass",examLabelClass);
                                    put("examIClass",examIClass);
                                }};
                                examMsgMapList.add(noticeMsgMap);
                            }
                        }else{
                            String examMsg = "距离"+realname+"的"+examInfo.getExamSite()+"的"+examInfo.getMain()+"时间还久";
                            //System.out.println(examMsg);
                            String examImg = "images/msg/exam1.png";
                            String examClass = "alert alert-success fade in";
                            String examLabelClass = "label label-success";
                            String examIClass = "fa fa-check";
                            ExamInfo finalExamInfo2 = examInfo;
                            noticeMsgMap = new HashMap<>(){{
                                put("examId", finalExamInfo2.getExamId());
                                put("examNotice",examMsg);
                                put("examImg",examImg);
                                put("examClass",examClass);
                                put("examLabelClass",examLabelClass);
                                put("examIClass",examIClass);
                            }};
                            examMsgMapList.add(noticeMsgMap);
                        }
                    }
                }//添加体检提醒
                List<MedicationInfo> medList = medicationService.selectmedication((Long) userMapList.get(i).get("userid"));//获取体检信息
                MedicationInfo medicationInfo;
                for (int j = 0; j <medList.toArray().length; j++) {
                    medicationInfo = medList.get(j);
                    java.util.Date medDate = medicationInfo.getMedicationTime();
                    if(medDate.getTime()-dateNow.getTime()<=1800000&&medicationInfo.getMedicationDone()!=1){
                        if(medDate.getTime()-dateNow.getTime()<0){
                            String medMsg = realname+"该服用"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"了";
                            //System.out.println(examMsg);
                            String medClass = " alert alert-block alert-danger fade in";
                            String medLabelClass = "label label-danger";
                            String medIClass = "fa fa-bolt";
                            String medImg = "images/msg/medical.png";
                            MedicationInfo finalMedicationInfo = medicationInfo;
                            noticeMsgMap = new HashMap<>(){{
                                put("medicationId", finalMedicationInfo.getMedicationId());
                                put("medMsg",medMsg);
                                put("medImg",medImg);
                                put("medClass",medClass);
                                put("medLabelClass",medLabelClass);
                                put("medIClass",medIClass);
                            }};
                            medMsgMapList.add(noticeMsgMap);
                        }
                        else {
                            long time = (medDate.getTime()-dateNow.getTime())/60000;
                            String medMsg = "还有"+time+"分钟"+realname+"就该服用"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"了";
                            //System.out.println(examMsg);
                            String medClass = "alert alert-warning fade in";
                            String medLabelClass = "label label-warning";
                            String medIClass = "fa fa-clock-o";
                            String medImg = "images/msg/medical.png";
                            MedicationInfo finalMedicationInfo1 = medicationInfo;
                            noticeMsgMap = new HashMap<>(){{
                                put("medicationId", finalMedicationInfo1.getMedicationId());
                                put("medMsg",medMsg);
                                put("medImg",medImg);
                                put("medClass",medClass);
                                put("medLabelClass",medLabelClass);
                                put("medIClass",medIClass);
                            }};
                            medMsgMapList.add(noticeMsgMap);
                        }
                    }else if(medicationInfo.getMedicationDone()!=1){
                        String medMsg = "距离"+realname+"的"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"服药时间还久";
                        //System.out.println(examMsg);
                        String medClass = "alert alert-success fade in";
                        String medLabelClass = "label label-success";
                        String medIClass = "fa fa-check";
                        String medImg = "images/msg/medical.png";
                        MedicationInfo finalMedicationInfo2 = medicationInfo;
                        noticeMsgMap = new HashMap<>(){{
                            put("medicationId", finalMedicationInfo2.getMedicationId());
                            put("medMsg",medMsg);
                            put("medImg",medImg);
                            put("medClass",medClass);
                            put("medLabelClass",medLabelClass);
                            put("medIClass",medIClass);
                        }};
                        medMsgMapList.add(noticeMsgMap);
                    }
                }//添加服药提醒
                List<ElderhealthInfo> healthList = healthInfoService.healthByid((Long)userMapList.get(i).get("userid"));
                Map<String,Object> healthMap;
                for (ElderhealthInfo health:healthList
                     ) {
                    healthMap = healthInfoController.sethealthMapList(health);
                    healthWarningMapList.add(healthMap);
                }//添加健康预警
            }
            Integer medNum = medMsgMapList.size();
            Integer examNum = examMsgMapList.size();
            Integer healthWarningNum = healthWarningMapList.size();
            session.setAttribute("medNum",medNum);
            session.setAttribute("examNum",examNum);
            session.setAttribute("healthWarningNum",healthWarningNum);
            session.setAttribute("medMsgMapList",medMsgMapList);
            session.setAttribute("examMsgMapList",examMsgMapList);
            session.setAttribute("healthWarningMapList",healthWarningMapList);
            return "main";
        }//子女用户消息提醒功能
        else if(checkuser.getRole().equals("父母")){
            List<ExamInfo> examList = examService.examByid(checkuser.getUserId());//获取体检信息
            ExamInfo examInfo;
            for (int j = 0; j <examList.toArray().length; j++) {
                examInfo = examList.get(j);
                java.util.Date examDate = examInfo.getExamDate();
                if (examInfo.getExamState()==0) {
                    if (examDate.getTime() - dateNow.getTime() <= 1800000) {
                        if (examDate.getTime() - dateNow.getTime() < 0) {
                            String examMsg = "该去" + examInfo.getExamSite() + "做" + examInfo.getMain() + "了";
                            //System.out.println(examMsg);
                            String examClass = " alert alert-block alert-danger fade in";
                            String examLabelClass = "label label-danger";
                            String examIClass = "fa fa-bolt";
                            String examImg = "images/msg/exam1.png";
                            ExamInfo finalExamInfo = examInfo;
                            noticeMsgMap = new HashMap<>() {{
                                put("examId", finalExamInfo.getExamId());
                                put("examNotice", examMsg);
                                put("examImg", examImg);
                                put("examClass", examClass);
                                put("examLabelClass", examLabelClass);
                                put("examIClass", examIClass);
                            }};
                            examMsgMapList.add(noticeMsgMap);
                        } else {
                            long time = (examDate.getTime() - dateNow.getTime()) / 60000;
                            String examMsg = "还有" + time + "分钟" + "就该去" + examInfo.getExamSite() + "做" + examInfo.getMain() + "了";
                            //System.out.println(examMsg);
                            String examImg = "images/msg/exam1.png";
                            String examClass = "alert alert-warning fade in";
                            String examLabelClass = "label label-warning";
                            String examIClass = "fa fa-clock-o";
                            ExamInfo finalExamInfo1 = examInfo;
                            noticeMsgMap = new HashMap<>() {{
                                put("examId", finalExamInfo1.getExamId());
                                put("examNotice", examMsg);
                                put("examImg", examImg);
                                put("examClass", examClass);
                                put("examLabelClass", examLabelClass);
                                put("examIClass", examIClass);
                            }};
                            examMsgMapList.add(noticeMsgMap);
                        }
                    } else {
                        String examMsg = "距离" + examInfo.getExamSite() + "的" + examInfo.getMain() + "时间还久";
                        //System.out.println(examMsg);
                        String examImg = "images/msg/exam1.png";
                        String examClass = "alert alert-success fade in";
                        String examLabelClass = "label label-success";
                        String examIClass = "fa fa-check";
                        ExamInfo finalExamInfo2 = examInfo;
                        noticeMsgMap = new HashMap<>() {{
                            put("examId", finalExamInfo2.getExamId());
                            put("examNotice", examMsg);
                            put("examImg", examImg);
                            put("examClass", examClass);
                            put("examLabelClass", examLabelClass);
                            put("examIClass", examIClass);
                        }};
                        examMsgMapList.add(noticeMsgMap);
                    }//添加体检消息
                }
            }
            List<MedicationInfo> medList = medicationService.selectmedication(checkuser.getUserId());//获取体检信息
            MedicationInfo medicationInfo;
            for (int j = 0; j <medList.toArray().length; j++) {
                medicationInfo = medList.get(j);
                java.util.Date medDate = medicationInfo.getMedicationTime();
                if(medDate.getTime()-dateNow.getTime()<=1800000&&medicationInfo.getMedicationDone()!=1){
                    if(medDate.getTime()-dateNow.getTime()<0){
                        String medMsg = "该服用"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"了";
                        //System.out.println(examMsg);
                        String medImg = "images/msg/medical.png";
                        String medClass = " alert alert-block alert-danger fade in";
                        String medLabelClass = "label label-danger";
                        String medIClass = "fa fa-bolt";
                        MedicationInfo finalMedicationInfo2 = medicationInfo;
                        noticeMsgMap = new HashMap<>(){{
                            put("medicationId", finalMedicationInfo2.getMedicationId());
                            put("medMsg",medMsg);
                            put("medImg",medImg);
                            put("medClass",medClass);
                            put("medLabelClass",medLabelClass);
                            put("medIClass",medIClass);
                        }};
                        medMsgMapList.add(noticeMsgMap);
                    }
                    else {
                        long time = (medDate.getTime()-dateNow.getTime())/60000;
                        String medMsg = "还有"+time+"分钟"+"就该服用"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"了";
                        //System.out.println(examMsg);
                        String medImg = "images/msg/medical.png";
                        String medClass = "alert alert-warning fade in";
                        String medLabelClass = "label label-warning";
                        String medIClass = "fa fa-clock-o";
                        MedicationInfo finalMedicationInfo = medicationInfo;
                        noticeMsgMap = new HashMap<>(){{
                            put("medicationId", finalMedicationInfo.getMedicationId());
                            put("medMsg",medMsg);
                            put("medImg",medImg);
                            put("medClass",medClass);
                            put("medLabelClass",medLabelClass);
                            put("medIClass",medIClass);
                        }};
                        medMsgMapList.add(noticeMsgMap);
                    }
                }else if(medicationInfo.getMedicationDone()!=1){
                    String medMsg = "距离"+medicationInfo.getMedicationDose()+medicationInfo.getMedicineName()+"服药时间还久";
                    //System.out.println(examMsg);
                    String medImg = "images/msg/medical.png";
                    String medClass = "alert alert-success fade in";
                    String medLabelClass = "label label-success";
                    String medIClass = "fa fa-check";
                    MedicationInfo finalMedicationInfo1 = medicationInfo;
                    noticeMsgMap = new HashMap<>(){{
                        put("medicationId", finalMedicationInfo1.getMedicationId());
                        put("medMsg",medMsg);
                        put("medImg",medImg);
                        put("medClass",medClass);
                        put("medLabelClass",medLabelClass);
                        put("medIClass",medIClass);
                    }};
                    medMsgMapList.add(noticeMsgMap);
                }
            }//添加服药提醒
            List<ElderhealthInfo> healthList = healthInfoService.healthByid(checkuser.getUserId());
            Map<String,Object> healthMap;
            for (ElderhealthInfo health:healthList) {
                healthMap = healthInfoController.sethealthMapList(health);
                healthWarningMapList.add(healthMap);
            }//添加健康预警
            Integer medNum = medMsgMapList.size();
            Integer examNum = examMsgMapList.size();
            Integer healthWarningNum = healthWarningMapList.size();
            session.setAttribute("medNum",medNum);
            session.setAttribute("examNum",examNum);
            session.setAttribute("healthWarningNum",healthWarningNum);
            session.setAttribute("medMsgMapList",medMsgMapList);
            session.setAttribute("examMsgMapList",examMsgMapList);
            session.setAttribute("healthWarningMapList",healthWarningMapList);
            return "main";
        }//父母用户消息提醒功能
        else {
            model.addAttribute("errormsg","主页消息提醒功能用户角色错误");
            return "redirect:/main.html";
            //重定向到main，防止表单重复提交
        }
    }

    /**
     * 清除预警信息
     * @param session
     * @return
     */
    @GetMapping("msgRemove")
    public String removeMsg(HttpSession session){
        session.removeAttribute("healthWarningMapList");
        session.removeAttribute("healthWarningNum");
        return "main";
    }
}
